package cn.wolfcode.controller;

import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.SysDictData;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by XuHangBin
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    private String prefix = "customer";

    @RequestMapping()
    public String customer(ModelMap modelMap){
        List<Customer> customerList = customerService.selectAll();
        modelMap.put("customerList",customerList);
        return prefix+"/view";
    }
    @RequestMapping("/add")
    public String add(ModelMap modelMap){
        // 查询 来源渠道/意向校区
        List<SysDictData> schoolTypeList = sysDictDataService.listAll("schoolType");
        System.out.println("schoolTypeList = " + schoolTypeList);
        modelMap.put("schoolTypeList",schoolTypeList);
        List<SysDictData> sourceTypeList = sysDictDataService.listAll("sourceType");
        System.out.println("sourceTypeList = " + sourceTypeList);
        modelMap.put("sourceTypeList",sourceTypeList);
        return prefix+"/add";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Customer customer){
        System.out.println("customer = " + customer);
        customerService.saveOrUpdate(customer);
        return "redirect:/customer";
    }

    @RequestMapping("/edit")
    public String edit(Long id, ModelMap modelMap){
        Customer customer = customerService.get(id);
        modelMap.put("customer",customer);
        List<SysDictData> schoolTypeList = sysDictDataService.listAll("schoolType");
        modelMap.put("schoolTypeList",schoolTypeList);
        List<SysDictData> sourceTypeList = sysDictDataService.listAll("sourceType");
        modelMap.put("sourceTypeList",sourceTypeList);
        return prefix + "/edit";
    }
}
